package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.PermissionTime;
import com.itheima.service.JurisdictionManagementService;
import org.springframework.web.bind.annotation.*;


//权限管理
@RequestMapping("/jurisdictionManagement")
@RestController
public class JurisdictionManagementController {

    @Reference
    private JurisdictionManagementService jurisdictionManagementService;

    //权限管理分页查询
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult pageResult = jurisdictionManagementService.findPage(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //新增 或 修改权限
    @PostMapping("/add")
    public Result add(@RequestBody PermissionTime permissionTime){
        //校验用户传入的用户名是否为空
        if(permissionTime.getName() == null || permissionTime.getName().equals("")){
            return new Result(false,"权限名称不能为空,请重新输入");
        }
        //校验关键字是否为空
        if(permissionTime.getKeyword() == null || permissionTime.getKeyword().equals("")){
            return new Result(false,"关键字不能为空,请重新输入");
        }
        Result result = null;
        try {
             result = jurisdictionManagementService.add(permissionTime);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }

    //编辑回显权限信息
    @GetMapping("/echoJurisdiction")
    public Result echoJurisdiction(Integer id){

        try {
            PermissionTime permissionTime = jurisdictionManagementService.echoJurisdiction(id);
            return new Result(true,"权限信息查询成功",permissionTime);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限信息查询失败");
        }
    }

    //删除权限
    @PostMapping("/deleteJurisdiction")
    public Result deleteJurisdiction(Integer id){
        Result result = null;
        try {
            result = jurisdictionManagementService.deleteJurisdiction(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }
}
